package com.android.firstgame;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.DragEvent;
import android.graphics.Paint;
import java.lang.Runnable;
import java.lang.Thread;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import java.util.ArrayList;
import android.graphics.BlurMaskFilter;
import android.graphics.BlurMaskFilter.Blur;

public class DrawView extends SurfaceView implements SurfaceHolder.Callback, Runnable{
    private Paint paint = new Paint();
	private float f_pointerx = 0;
	private float f_pointery = 0;
	//for draw debug string
	private int i_debug_string_posy = -5;
	//for control vpad
	private float i_move_small_circle_x;
	private float i_move_small_circle_y;
	private float i_fight_small_circle_x;
	private float i_fight_small_circle_y;
	private static float I_STATIC_MOVE_SMALL_CIRCLE_X;
	private static float I_STATIC_MOVE_SMALL_CIRCLE_Y;
	private static float I_STATIC_FIGHT_SMALL_CIRCLE_X;
	private static float I_STATIC_FIGHT_SMALL_CIRCLE_Y;
	private static int I_STATIC_SMALL_CIRCLE_RADIUS;
	//for screen	
	private static int I_SCREEN_WIDTH;
	private static int I_SCREEN_HEIGHT;
	//for MC
	private Actor o_main_character;
	private float f_move_touched_posx, f_move_touched_posy, f_fight_touched_posx, f_fight_touched_posy;
	private static boolean b_is_mc_hitted = false;
	
	
	private static int F_GUN_OVERLOAD_TIME = 10000;
	
	//for input	
	private int i_touched_pointer_count;
	private int i_move_touched_pointerid, i_fight_touched_pointerid;
	private int i_move_touched_pointerindex, i_fight_touched_pointerindex;
	private double f_move_distance_length, f_fight_distance_length;
	private int i_pause_button_posx, i_pause_button_posy, i_pause_button_size;
	
	//for debug
	private int[] input_pointer_array, input_pointer_array_id;
	private float[] input_pointer_array_x, input_pointer_array_y;
	
	//for bullets
    private ArrayList<Bullet> bulletlist;
	private float f_gun_virtual_target_x, f_gun_virtual_target_y;
	private int i_bullet_delay;
	
	//for background
	private int[] i_background_vertical_lines_x;
	private int[] i_background_horizontal_lines_y;
	private static int I_MAX_VERTICAL_LINE, I_MAX_HORIZONTAL_LINE;
	private int i_map_move_x_offset, i_map_move_y_offset;
	
	//for enemy
	private ArrayList<Actor> enemylist;
	
	//holder
	private SurfaceHolder o_surfaceholder;
	//main game thread
	private Thread o_game_mainthread;
	private boolean i_thread_can_run = false;
	
	//use to draw
	private static float f_old_strokewidth;
	private static float f_old_textsize;
	//logo screen
	private float f_logo_text_size = 0;//12 is default
	private static final int I_LOGO_STATE0 = 0;
	private static final int I_LOGO_STATE1 = I_LOGO_STATE0 + 1;
	private static final int I_LOGO_STATE2 = I_LOGO_STATE1 + 1;
	private static final int I_LOGO_STATE3 = I_LOGO_STATE2 + 1;
	private static final int I_LOGO_STATE4 = I_LOGO_STATE3 + 1;
	private static final int I_LOGO_STATE5 = I_LOGO_STATE4 + 1;
	private static final int I_LOGO_STATE6 = I_LOGO_STATE5 + 1;
	
	//intro screen
	private static int i_vertical_line_intro_offsety, i_horizontal_line_intro_offsetx;
	private int i_intro_mc_delay_posy;
	private int i_intro_mc_posy, i_intro_quit_button_posx, i_intro_option_button_posx, i_intro_play_button_posy, i_intro_title_posy;
	private static final int I_INTRO_STATE0 = 0;
	private static final int I_INTRO_STATE1 = I_INTRO_STATE0 + 1;
	private static final int I_INTRO_STATE2 = I_INTRO_STATE1 + 1;
	private static final int I_INTRO_STATE3 = I_INTRO_STATE2 + 1;
	private static final int I_INTRO_STATE4 = I_INTRO_STATE3 + 1;
	private static final int I_INTRO_STATE5 = I_INTRO_STATE4 + 1;
	private static final int I_INTRO_STATE6 = I_INTRO_STATE5 + 1;
	
	//for MM
	private int i_quit_option_button_width = 15, i_play_button_radius = 30, i_title_font_size = 30;
	
	//for game screens
	private int i_current_screen;
	private int i_current_state;
	private static double d_screen_start_time = 0;
	private static int i_screen_frame_count;
	private static int i_state_frame_count;
	private static final int I_SCREEN_LOGO = 0;
	private static final int I_SCREEN_INTRO = I_SCREEN_LOGO + 1;
	private static final int I_SCREEN_MAIN_MENU = I_SCREEN_INTRO + 1;
	private static final int I_SCREEN_HELP = I_SCREEN_MAIN_MENU + 1;
	private static final int I_SCREEN_HIGHT_SCORE = I_SCREEN_HELP + 1;
	private static final int I_SCREEN_GAMEPLAY = I_SCREEN_HIGHT_SCORE + 1;
	private static final int I_SCREEN_INGAME_MENU = I_SCREEN_GAMEPLAY + 1;
	
	//for control game thread
	private boolean b_is_game_pause = false;//use for interrupt and no control in game play
	
	//for dummy enemy
	private Actor o_dummy_enemy;
	
	float[] f_mc_enemy_collision_x, f_mc_enemy_collision_y;
	
	//rada
	float[] f_enemy_rada_x, f_enemy_rada_y;
	
	
    public DrawView(Context context){
        super(context);
		o_surfaceholder = getHolder();
		o_surfaceholder.addCallback(this);
		setFocusable(true);
		I_SCREEN_WIDTH = context.getResources().getDisplayMetrics().widthPixels;
		I_SCREEN_HEIGHT = context.getResources().getDisplayMetrics().heightPixels;
    }
	
	public void Initialize(){
		// ini control
		I_STATIC_SMALL_CIRCLE_RADIUS = I_SCREEN_WIDTH/32;
		I_STATIC_MOVE_SMALL_CIRCLE_X = i_move_small_circle_x = I_SCREEN_WIDTH/6;
		I_STATIC_FIGHT_SMALL_CIRCLE_X = i_fight_small_circle_x  = I_SCREEN_WIDTH - I_SCREEN_WIDTH/6;
		I_STATIC_MOVE_SMALL_CIRCLE_Y = i_move_small_circle_y = I_STATIC_FIGHT_SMALL_CIRCLE_Y = i_fight_small_circle_y = I_SCREEN_HEIGHT - I_SCREEN_HEIGHT/4;		
		//ini MC
		i_intro_mc_posy = I_SCREEN_HEIGHT/2;
		// i_intro_mc_posy = I_SCREEN_HEIGHT + (I_SCREEN_WIDTH/64 + I_SCREEN_WIDTH/128)*2;// use to draw intro
		o_main_character = new Actor(I_SCREEN_WIDTH/2, i_intro_mc_posy, 0, I_SCREEN_WIDTH/64 + I_SCREEN_WIDTH/128);
		o_dummy_enemy = new Actor(I_SCREEN_WIDTH/2 + I_SCREEN_WIDTH/4, I_SCREEN_WIDTH/4, 0, I_SCREEN_WIDTH/64 + I_SCREEN_WIDTH/128);

		//ini input	
		i_touched_pointer_count = 0;
		f_move_touched_posx = -1;
		f_move_touched_posy = -1;
		f_fight_touched_posx = -1;
		f_fight_touched_posy = -1;	
		i_move_touched_pointerindex = -1;
		i_fight_touched_pointerindex = -1;
		i_move_touched_pointerid = -1;
		i_fight_touched_pointerid = -1;
		//for debug
		input_pointer_array = new int[1];
		input_pointer_array_id = new int[1];
		input_pointer_array_x = new float[1];
		input_pointer_array_y = new float[1];
		//for bullets
		bulletlist = new ArrayList<Bullet>();
		f_gun_virtual_target_x = 0;
		f_gun_virtual_target_y = 0;
		i_bullet_delay = 2;
		//for enemy
		enemylist = new ArrayList<Actor>();
		enemylist.add(o_dummy_enemy);
		//for background
		I_MAX_VERTICAL_LINE = 5;
		I_MAX_HORIZONTAL_LINE = I_SCREEN_HEIGHT/(I_SCREEN_WIDTH/I_MAX_VERTICAL_LINE);
		i_background_vertical_lines_x = new int[I_MAX_VERTICAL_LINE];
		i_background_horizontal_lines_y = new int[I_MAX_HORIZONTAL_LINE];
		i_background_vertical_lines_x[0] = (int)I_SCREEN_WIDTH/(2*I_MAX_VERTICAL_LINE);
		i_background_horizontal_lines_y[0] = (int)I_SCREEN_HEIGHT/(2*I_MAX_HORIZONTAL_LINE);
		for(int i = 1; i < I_MAX_VERTICAL_LINE; i++){
			i_background_vertical_lines_x[i] = i_background_vertical_lines_x[i-1] + (I_SCREEN_WIDTH/I_MAX_VERTICAL_LINE);
		}
		for(int i = 1; i < I_MAX_HORIZONTAL_LINE; i++){
			i_background_horizontal_lines_y[i] = i_background_horizontal_lines_y[i-1] + (I_SCREEN_HEIGHT/I_MAX_HORIZONTAL_LINE);
		}
		i_map_move_x_offset = 0;
		i_map_move_y_offset = 0;
		//game main thread
		o_game_mainthread = new Thread(this);
		o_game_mainthread.start();
		i_thread_can_run = true;
		
		//ini game screens		
		i_current_screen = I_SCREEN_GAMEPLAY;
		i_current_state = I_LOGO_STATE0;
		i_screen_frame_count =0;
		i_state_frame_count = 0;
		//intro
		i_vertical_line_intro_offsety = 0;
		i_horizontal_line_intro_offsetx = 0;		
		i_intro_mc_delay_posy = 10;
		i_intro_quit_button_posx = I_SCREEN_WIDTH + i_quit_option_button_width + 5;
		i_intro_option_button_posx = -i_quit_option_button_width - 5;
		i_intro_play_button_posy = I_SCREEN_HEIGHT + I_SCREEN_HEIGHT*2 + 5;
		// i_intro_title_posy = 
		
		//for rada
		f_enemy_rada_x = new float[1];
		f_enemy_rada_y = new float[1];
		f_mc_enemy_collision_x = new float[1];
		f_mc_enemy_collision_y = new float[1];
		
	}

    @Override
    protected void onDraw(Canvas canvas){
		Game_Draw(canvas);		
    }
	public int getCurrentScreen(){
		return i_current_screen;
	}
	private void Game_Update(){
		Update_Background();
		Update_Control();
		Update_MC();
		Update_Bullets();
		Update_Enemy();
		Update_Collision();
	}
	
	private void Update_Background(){
		switch(i_current_screen){
			case I_SCREEN_INTRO:
				i_state_frame_count ++;
				if(i_state_frame_count < 20){		
					i_current_state = I_INTRO_STATE0;
				}else if(i_state_frame_count == 20){
					i_current_state = I_INTRO_STATE1;
				}				
				if(i_current_state == I_INTRO_STATE1){
					i_vertical_line_intro_offsety += I_SCREEN_HEIGHT/20;
					i_horizontal_line_intro_offsetx += I_SCREEN_WIDTH/20;
					if(i_vertical_line_intro_offsety >= I_SCREEN_WIDTH){
						i_current_state = I_INTRO_STATE2;
					}
				}
			break;		
		}
		if(i_move_touched_pointerid != -1){
			if(!b_is_mc_hitted){
				i_background_vertical_lines_x[0] -= i_map_move_x_offset;//vi la background di chuyen nen tru chu ko phai cong nhu mc
				i_background_horizontal_lines_y[0] -= i_map_move_y_offset;//tuong tu nhu tren
			}
			if(i_background_vertical_lines_x[0] < 0){
				i_background_vertical_lines_x[0] = (I_SCREEN_WIDTH/I_MAX_VERTICAL_LINE) + i_background_vertical_lines_x[0];
			}else if(i_background_vertical_lines_x[0] > (I_SCREEN_WIDTH/I_MAX_VERTICAL_LINE)){
				i_background_vertical_lines_x[0] = i_background_vertical_lines_x[0] - (I_SCREEN_WIDTH/I_MAX_VERTICAL_LINE);
			}
			if(i_background_horizontal_lines_y[0] < 0){
				i_background_horizontal_lines_y[0] = (I_SCREEN_HEIGHT/I_MAX_HORIZONTAL_LINE) + i_background_horizontal_lines_y[0];
			}else if(i_background_horizontal_lines_y[0] > (I_SCREEN_HEIGHT/I_MAX_HORIZONTAL_LINE)){
				i_background_horizontal_lines_y[0] = i_background_horizontal_lines_y[0] - (I_SCREEN_HEIGHT/I_MAX_HORIZONTAL_LINE);
			}
			for(int j = 1; j < I_MAX_VERTICAL_LINE; j++){
				i_background_vertical_lines_x[j] = i_background_vertical_lines_x[j-1] + (I_SCREEN_WIDTH/I_MAX_VERTICAL_LINE);
			}
			for(int j = 1; j < I_MAX_HORIZONTAL_LINE; j++){
				i_background_horizontal_lines_y[j] = i_background_horizontal_lines_y[j-1] + (I_SCREEN_HEIGHT/I_MAX_HORIZONTAL_LINE);
			}
		}
	}
	
	private void Update_Control(){
		switch(i_current_screen){
			case I_SCREEN_INTRO:
				switch(i_current_state){
					case I_INTRO_STATE3:
						if(i_intro_quit_button_posx >= I_SCREEN_WIDTH - i_quit_option_button_width - 25)
							i_intro_quit_button_posx -= 2;
						if(i_intro_option_button_posx <= i_quit_option_button_width + 25)
							i_intro_option_button_posx += 2;
					break;
				}
			break;
		}
	}
	
	private void Update_MC(){
		switch(i_current_screen){
			case I_SCREEN_INTRO:
				switch(i_current_state){
					case I_INTRO_STATE2:
						if(i_intro_mc_posy > I_SCREEN_HEIGHT/2){
							if(i_intro_mc_posy > I_SCREEN_HEIGHT/2 + 55){
								i_intro_mc_posy -= 10;
							}else{
								if(i_intro_mc_delay_posy == 0)
									i_current_state = I_INTRO_STATE3;
								else{
									i_intro_mc_delay_posy --;
								i_intro_mc_posy -= i_intro_mc_delay_posy;
								}
							}							
						}
						o_main_character.setPosition(I_SCREEN_WIDTH/2, i_intro_mc_posy);
					break;
				}
			break;
		}
	}
	
	private void Update_Bullets(){
		//calculate for gun
		for(int i = 0; i < bulletlist.size(); i ++){
			if(bulletlist.get(i).checkvisible(I_SCREEN_WIDTH, I_SCREEN_HEIGHT) != true){
				try {
					bulletlist.get(i).finalize();
				} catch (Throwable ex) {
					ex.printStackTrace();
				}
				bulletlist.remove(i);
			}
		}
		try{
			for(int i = 0; i < bulletlist.size(); i ++){
				bulletlist.get(i).relocation();
				if(i_move_touched_pointerindex != -1){
					// bulletlist.get(i).setBulletX(bulletlist.get(i).getBulletX() - i_map_move_x_offset);
					// bulletlist.get(i).setBulletY(bulletlist.get(i).getBulletY() - i_map_move_y_offset);
				}
				
			}
		}		
		catch(Exception e){}
	}
	
	private void Update_Enemy(){
		//for enemy
		if(enemylist.size() < 1){
			// enemylist.add(new Actor(200, 200, 1, I_SCREEN_WIDTH/32));
		}
		for(int i = 0; i < enemylist.size(); i++){
			if(i_move_touched_pointerid != -1){
				if(!b_is_mc_hitted){
					enemylist.get(i).setPosition(enemylist.get(i).getX() - i_map_move_x_offset, enemylist.get(i).getY() - i_map_move_y_offset);
				}
			}
			if(enemylist.get(i).isDead()){
				
			}
		}
	}
	
	private void Update_Collision(){
		//include check score
		//check MC and enemy collision
		// float f_enemy_rada_x, f_enemy_rada_y;
		double mc_enemy_angle;
		f_enemy_rada_x = new float[enemylist.size()];
		f_enemy_rada_y = new float[enemylist.size()];
		f_mc_enemy_collision_x = new float[enemylist.size()];
		f_mc_enemy_collision_y = new float[enemylist.size()];
		for(int i = 0; i < enemylist.size(); i++){
			mc_enemy_angle = Math.atan2(enemylist.get(i).getX() - o_main_character.getX(), enemylist.get(i).getY() - o_main_character.getY());
			f_enemy_rada_x[i] = o_main_character.getX() + (o_main_character.getActorRadius() + o_main_character.getActorRadius()/2)*(float)Math.sin(mc_enemy_angle);
			f_enemy_rada_y[i] = o_main_character.getY() + (o_main_character.getActorRadius() + o_main_character.getActorRadius()/2)*(float)Math.cos(mc_enemy_angle);
			f_mc_enemy_collision_x[i] = o_main_character.getX() + o_main_character.getActorRadius()*(float)Math.sin(mc_enemy_angle);
			f_mc_enemy_collision_y[i] = o_main_character.getY() + o_main_character.getActorRadius()*(float)Math.cos(mc_enemy_angle);
			double d_mc_enemy_distance = Math.sqrt((enemylist.get(i).getX() - o_main_character.getX())*(enemylist.get(i).getX() - o_main_character.getX())+(enemylist.get(i).getY() - o_main_character.getY())*(enemylist.get(i).getY() - o_main_character.getY()));
			if(d_mc_enemy_distance <= (o_main_character.getActorRadius() + enemylist.get(i).getActorRadius())){
				b_is_mc_hitted = true;
				// o_main_character.setPosition();
				// b_is_enemy_hitted[i] = true;
			}else{
				b_is_mc_hitted = false;
			}
		}
	}
	
	private void Game_Draw(Canvas canvas){		
        Draw_Background(canvas);		
		Draw_MC(canvas);
		Draw_Bullets(canvas);
		Draw_Enemys(canvas);
		Draw_Control(canvas);
		Draw_Debug(canvas);	
	}
	
	private void Draw_Background(Canvas canvas){
		switch(i_current_screen){
			case I_SCREEN_LOGO:		
				canvas.drawColor(Color.BLACK);
				paint.setColor(Color.WHITE);
				f_old_textsize = paint.getTextSize();
				
				// if(f_logo_text_size < I_SCREEN_WIDTH/8)
					// f_logo_text_size +=10;
				// paint.setTextSize(f_logo_text_size);
				// canvas.drawText("Always Free", I_SCREEN_WIDTH/2 - f_logo_text_size*3, I_SCREEN_HEIGHT/2 - f_logo_text_size, paint);
				// canvas.drawText("Studio", I_SCREEN_WIDTH/2 - f_logo_text_size*2, I_SCREEN_HEIGHT/2 + f_logo_text_size, paint);
				
				
				paint.setTextSize((I_SCREEN_WIDTH/8));
				canvas.drawText("Always Free", I_SCREEN_WIDTH/2 - I_SCREEN_WIDTH/3, I_SCREEN_HEIGHT/2 - I_SCREEN_HEIGHT/8, paint);
				canvas.drawText("Studio", I_SCREEN_WIDTH/2 - I_SCREEN_WIDTH/6, I_SCREEN_HEIGHT/2 + I_SCREEN_HEIGHT/8, paint);
				paint.setTextSize(f_old_textsize);
				i_screen_frame_count ++;
				if(i_screen_frame_count == 100)
					i_current_screen = I_SCREEN_INTRO;
			break;
			case I_SCREEN_INTRO:
				switch(i_current_state){
					case I_INTRO_STATE0:
						canvas.drawColor(Color.BLACK);
					break;
					case I_INTRO_STATE1:
						canvas.drawColor(Color.BLACK);
						paint.setColor(Color.GREEN);
						for(int i = 0; i < I_MAX_VERTICAL_LINE; i ++){
							canvas.drawLine(i_background_vertical_lines_x[i], 0, i_background_vertical_lines_x[i], i_vertical_line_intro_offsety, paint);
						}
						for(int i = 0; i < I_MAX_HORIZONTAL_LINE; i++){
							canvas.drawLine(0, i_background_horizontal_lines_y[i], i_horizontal_line_intro_offsetx, i_background_horizontal_lines_y[i], paint);
						}
					break;
					case I_INTRO_STATE2:
						canvas.drawColor(Color.BLUE);
						paint.setColor(Color.GREEN);
						for(int i = 0; i < I_MAX_VERTICAL_LINE; i ++){
							canvas.drawLine(i_background_vertical_lines_x[i], 0, i_background_vertical_lines_x[i], I_SCREEN_HEIGHT, paint);
						}
						for(int i = 0; i < I_MAX_HORIZONTAL_LINE; i++){
							canvas.drawLine(0, i_background_horizontal_lines_y[i], I_SCREEN_WIDTH, i_background_horizontal_lines_y[i], paint);
						}
					break;
				}
			break;
			case I_SCREEN_MAIN_MENU:
			case I_SCREEN_GAMEPLAY:
			// paint.setAlpha(150);//use to draw floating score
			canvas.drawColor(Color.BLACK);        
			paint.setColor(Color.GREEN);
			for(int i = 0; i < I_MAX_VERTICAL_LINE; i ++){
				canvas.drawLine(i_background_vertical_lines_x[i], 0, i_background_vertical_lines_x[i], I_SCREEN_HEIGHT, paint);
			}
			for(int i = 0; i < I_MAX_HORIZONTAL_LINE; i++){
				canvas.drawLine(0, i_background_horizontal_lines_y[i], I_SCREEN_WIDTH, i_background_horizontal_lines_y[i], paint);
			}
			break;
			case I_SCREEN_INGAME_MENU://use draw buffer to draw game background in IGM
			break;
		}
	}
	
	private void Draw_MC(Canvas canvas){
		switch(i_current_screen){
			case I_SCREEN_LOGO:
				break;				
			case I_SCREEN_INTRO:			
			case I_SCREEN_MAIN_MENU:
			case I_SCREEN_GAMEPLAY:
				//draw MC
				if(b_is_mc_hitted){
					paint.setColor(Color.YELLOW);
					// b_is_mc_hitted = false;
				}else{
					paint.setColor(Color.WHITE);
				}
				paint.setMaskFilter(new BlurMaskFilter(10, BlurMaskFilter.Blur.NORMAL));
				canvas.drawCircle(o_main_character.getX(), o_main_character.getY(), o_main_character.getActorRadius(), paint);
				paint.setMaskFilter(null);
				canvas.drawCircle(o_main_character.getX(), o_main_character.getY(), o_main_character.getActorRadius(), paint);
				f_old_strokewidth = paint.getStrokeWidth();
				paint.setStrokeWidth(3f);
				canvas.drawLine(o_main_character.getX(), o_main_character.getY(), o_main_character.getGunPosX(), o_main_character.getGunPosY(), paint);
				paint.setStrokeWidth(f_old_strokewidth);
				//draw rada
				for(int i = 0; i < enemylist.size(); i++){
					paint.setColor(Color.RED);
					canvas.drawRect(f_enemy_rada_x[i] - 2, f_enemy_rada_y[i] - 2, f_enemy_rada_x[i] + 2, f_enemy_rada_y[i] + 2, paint);
				}
			break;
		}
	}
	
	private void Draw_Enemys(Canvas canvas){
		switch(i_current_screen){
			case I_SCREEN_LOGO:
				break;
			case I_SCREEN_INTRO:
				
			break;
			case I_SCREEN_MAIN_MENU:
			case I_SCREEN_GAMEPLAY:
				//draw enemy
				paint.setColor(Color.RED);
				try{
					for(int i = 0; i < enemylist.size(); i ++){
						canvas.drawCircle(enemylist.get(i).getX(), enemylist.get(i).getY(), enemylist.get(i).getActorRadius(), paint);
					}
				}catch(Exception e){}
			break;
		}
	}
	
	private	void Draw_Bullets(Canvas canvas){
		switch(i_current_screen){
			case I_SCREEN_LOGO:
				break;
			case I_SCREEN_INTRO:
				
			break;
			case I_SCREEN_MAIN_MENU:
			case I_SCREEN_GAMEPLAY:
				//draw bullet
				try{
					for(int i = 0; i < bulletlist.size(); i ++)
						canvas.drawRect(bulletlist.get(i).getBulletX() - 1, bulletlist.get(i).getBulletY() - 1, bulletlist.get(i).getBulletX() + 1, bulletlist.get(i).getBulletY() + 1, paint);
				}catch(Exception e){}
			break;
		}
	}
	
	private void Draw_Score(Canvas canvas){
	
	}
	
	private void Draw_Control(Canvas canvas){
		switch(i_current_screen){
			case I_SCREEN_LOGO:
				break;
			case I_SCREEN_INTRO:
				switch(i_current_state){
					case I_INTRO_STATE3:
						paint.setColor(Color.RED);
						canvas.drawRect(i_intro_quit_button_posx, I_SCREEN_HEIGHT - 70, I_SCREEN_WIDTH - 25, i_intro_quit_button_posx + i_quit_option_button_width, paint);
						paint.setColor(Color.BLACK);
						paint.setStrokeWidth(3f);
						canvas.drawText("Quit", i_intro_quit_button_posx + 5, I_SCREEN_HEIGHT - 65, paint);

						// if(i_intro_option_button_posx < i_quit_option_button_width + 5)
							// i_intro_option_button_posx += 2;
					break;
				}
			break;
			case I_SCREEN_MAIN_MENU:
			case I_SCREEN_GAMEPLAY:
				f_old_strokewidth = paint.getStrokeWidth();
				paint.setStrokeWidth(3f);
				//move control
				paint.setStyle(Paint.Style.FILL);
				paint.setColor(Color.BLACK);
				canvas.drawCircle(I_STATIC_MOVE_SMALL_CIRCLE_X, I_STATIC_MOVE_SMALL_CIRCLE_Y, I_STATIC_SMALL_CIRCLE_RADIUS*2, paint);
				paint.setStyle(Paint.Style.STROKE);//set style for paint stroke only
				paint.setColor(Color.GREEN);
				canvas.drawCircle(I_STATIC_MOVE_SMALL_CIRCLE_X, I_STATIC_MOVE_SMALL_CIRCLE_Y, I_STATIC_SMALL_CIRCLE_RADIUS, paint);		
				paint.setColor(Color.GRAY);
				canvas.drawCircle(I_STATIC_MOVE_SMALL_CIRCLE_X, I_STATIC_MOVE_SMALL_CIRCLE_Y, I_STATIC_SMALL_CIRCLE_RADIUS*2, paint);
				paint.setColor(Color.YELLOW);
				canvas.drawCircle(i_move_small_circle_x, i_move_small_circle_y, I_STATIC_SMALL_CIRCLE_RADIUS, paint);		
				//fight control
				paint.setStyle(Paint.Style.FILL);
				paint.setColor(Color.BLACK);
				canvas.drawCircle(I_STATIC_FIGHT_SMALL_CIRCLE_X, I_STATIC_FIGHT_SMALL_CIRCLE_Y, I_STATIC_SMALL_CIRCLE_RADIUS*2, paint);
				paint.setStyle(Paint.Style.STROKE);
				paint.setColor(Color.BLUE);
				canvas.drawCircle(I_STATIC_FIGHT_SMALL_CIRCLE_X, I_STATIC_FIGHT_SMALL_CIRCLE_Y, I_STATIC_SMALL_CIRCLE_RADIUS, paint);
				paint.setColor(Color.GRAY);
				canvas.drawCircle(I_STATIC_FIGHT_SMALL_CIRCLE_X, I_STATIC_FIGHT_SMALL_CIRCLE_Y, I_STATIC_SMALL_CIRCLE_RADIUS*2, paint);
				paint.setColor(Color.RED);
				canvas.drawCircle(i_fight_small_circle_x, i_fight_small_circle_y, I_STATIC_SMALL_CIRCLE_RADIUS, paint);		
				paint.setStyle(Paint.Style.FILL);//reset style for fill shape
				paint.setStrokeWidth(f_old_strokewidth);
			break;
		}
	}
	
	private void Draw_Debug(Canvas canvas){
		i_debug_string_posy = 0;
		//draw debug string
		paint.setColor(Color.WHITE);
		// canvas.drawText("X: " + f_pointerx, 5, i_debug_string_posy += 15, paint);
		// canvas.drawText("Y: " + f_pointery, 5, i_debug_string_posy += 15, paint);
		canvas.drawText("Width: " + I_SCREEN_WIDTH, I_SCREEN_WIDTH*3/4, i_debug_string_posy += 15, paint);
		canvas.drawText("Height: " + I_SCREEN_HEIGHT, I_SCREEN_WIDTH*3/4, i_debug_string_posy += 15, paint);		
		canvas.drawText("Pointer Count: " + i_touched_pointer_count, I_SCREEN_WIDTH*3/4,  i_debug_string_posy+= 15, paint);
		canvas.drawText("Move: " + i_move_touched_pointerindex + ", " + i_move_touched_pointerid + ", " + (int)f_move_touched_posx + ", " + (int)f_move_touched_posy, I_SCREEN_WIDTH*3/4,  i_debug_string_posy+= 15, paint);
		canvas.drawText("Fight: " + i_fight_touched_pointerindex + ", " + i_fight_touched_pointerid + ", " + (int)f_fight_touched_posx + ", " + (int)f_fight_touched_posy, I_SCREEN_WIDTH*3/4,  i_debug_string_posy+= 15, paint);			
		// canvas.drawText("POs X:  "+ i_intro_quit_button_posx, 5, i_debug_string_posy+= 15, paint);
		// canvas.drawText("Actor Y:  "+ o_main_character.getY(), 5, i_debug_string_posy+= 15, paint);
		// canvas.drawText("down time: "+ downtime, 5, i_debug_string_posy+= 15, paint);
		if(i_touched_pointer_count > 0){
			for(int i = 0; i < i_touched_pointer_count; i ++){
				canvas.drawText("Index: " + i + ", ID: " + input_pointer_array_id[i] + ", X: " + (int)input_pointer_array_x[i] + ", Y: " + (int)input_pointer_array_y[i], I_SCREEN_WIDTH*3/4,  i_debug_string_posy+= 15, paint);
			}
		}
		if(b_is_mc_hitted){
			// b_is_mc_hitted = false;
			canvas.drawText("HITTTTTTTTTTTT", I_SCREEN_WIDTH*3/4,  i_debug_string_posy+= 15, paint);
		}
		
		// canvas.drawText("ID1: " + i_move_touched_pointerid, 5,  i_debug_string_posy+= 15, paint);
		// canvas.drawText("ID2: " + i_fight_touched_pointerid, 5,  i_debug_string_posy+= 15, paint);
		// canvas.drawText("Index1: " + i_move_touched_pointerindex, 5,  i_debug_string_posy+= 15, paint);
		// canvas.drawText("Index2: " + i_fight_touched_pointerindex, 5,  i_debug_string_posy+= 15, paint);
	}
	
	private void Process_TouchEvent(MotionEvent e){
		switch(i_current_screen){
			case I_SCREEN_LOGO:				
			case I_SCREEN_INTRO:				
			break;
			case I_SCREEN_MAIN_MENU:
			
			break;
			case I_SCREEN_GAMEPLAY:
				i_touched_pointer_count = e.getPointerCount();
				if(e.getActionMasked() == MotionEvent.ACTION_UP || e.getActionMasked() == MotionEvent.ACTION_POINTER_UP){
					// System.out.println("MotionEvent.ACTION_UP ============================= " + e.getActionIndex() + ", " + e.getPointerId(e.getActionIndex()));
					if(e.getActionIndex() == e.findPointerIndex(i_move_touched_pointerid)){			
						// System.out.println("Move releaseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
						//find new pointer ID if available
						int i_old_move_touched_pointerid = i_move_touched_pointerid;
						for(int i = 0; i < i_touched_pointer_count; i++){
							if(e.getX(i) < I_SCREEN_WIDTH/2 && e.getPointerId(i) != i_old_move_touched_pointerid){
								i_move_touched_pointerindex = i;
								i_move_touched_pointerid = e.getPointerId(i);
								// System.out.println("Found new move control " + i_move_touched_pointerindex + ", " + i_move_touched_pointerid);
								break;
							}
							i_move_touched_pointerid = -1;
							i_move_touched_pointerindex = -1;
							i_move_small_circle_x = I_STATIC_MOVE_SMALL_CIRCLE_X;
							i_move_small_circle_y = I_STATIC_MOVE_SMALL_CIRCLE_Y;
							f_move_touched_posx = -1;
							f_move_touched_posy = -1;
						}	
						
						// System.out.println("Move Index1: " + i_move_touched_pointerindex + ", " + i_move_touched_pointerid);
						// System.out.println("Fight Index1: " + i_fight_touched_pointerindex + ", " + i_fight_touched_pointerid);				
					}			
					if(e.getActionIndex() == e.findPointerIndex(i_fight_touched_pointerid)){			
						// System.out.println("Fight releaseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
						//find new pointer ID if available
						int i_old_fight_touched_pointerid = i_fight_touched_pointerid;
						for(int i = 0; i < i_touched_pointer_count; i++){
							if(e.getX(i) >= I_SCREEN_WIDTH/2 && e.getPointerId(i) != i_old_fight_touched_pointerid){
								// System.out.println("Found new fight control 1 " + i + ", " + e.getPointerId(i));
								// System.out.println("Found new fight control 2 " + i_fight_touched_pointerindex + ", " + i_fight_touched_pointerid);
								i_fight_touched_pointerindex = i;
								i_fight_touched_pointerid = e.getPointerId(i);
								break;
							}
							i_fight_touched_pointerid = -1;
							i_fight_touched_pointerindex = -1;
							i_fight_small_circle_x = I_STATIC_FIGHT_SMALL_CIRCLE_X;
							i_fight_small_circle_y = I_STATIC_FIGHT_SMALL_CIRCLE_Y;
							f_fight_touched_posx = -1;
							f_fight_touched_posy = -1;
						}	
						
						// System.out.println("Move Index2: " + i_move_touched_pointerindex + ", " + i_move_touched_pointerid);
						// System.out.println("Fight Index2: " + i_fight_touched_pointerindex + ", " + i_fight_touched_pointerid);
					}
					if(i_touched_pointer_count == 1)
						i_touched_pointer_count = 0;
				}
				
				if(e.getActionMasked() == MotionEvent.ACTION_DOWN || e.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN){
					// System.out.println("MotionEvent.ACTION_DOWN ============================= " + e.getActionIndex() + ", " + e.getPointerId(e.getActionIndex()));
					if(e.getX(e.getActionIndex()) < I_SCREEN_WIDTH/2 && i_move_touched_pointerid == -1){
						i_move_touched_pointerindex = e.getActionIndex();
						i_move_touched_pointerid = e.getPointerId(i_move_touched_pointerindex);
						// System.out.println("Moveeeeeeeeee " + i_move_touched_pointerindex + ", " + i_move_touched_pointerid);
					}			
					if(e.getX(e.getActionIndex()) >= I_SCREEN_WIDTH/2 && i_fight_touched_pointerid == -1){
						i_fight_touched_pointerindex = e.getActionIndex();
						i_fight_touched_pointerid = e.getPointerId(i_fight_touched_pointerindex);
						// System.out.println("Fightttttttt " + i_fight_touched_pointerindex + ", " + i_fight_touched_pointerid);
					}
				}
				if(e.getActionMasked() == MotionEvent.ACTION_MOVE){
					//refresh pointer index
					i_move_touched_pointerindex = e.findPointerIndex(i_move_touched_pointerid);
					i_fight_touched_pointerindex = e.findPointerIndex(i_fight_touched_pointerid);
					if(i_move_touched_pointerindex != -1){
						// System.out.println("Move controlingggggggggggggggggggggggggggggggggggggg");				
						// System.out.println("Move Index 3: " + i_move_touched_pointerindex + ", " + i_move_touched_pointerid);
						// System.out.println("MotionEvent.ACTION_MOVE 1 " + e.getActionIndex() + ", " + e.getPointerId(e.getActionIndex()));
						f_move_touched_posx = e.getX(i_move_touched_pointerindex);
						f_move_touched_posy = e.getY(i_move_touched_pointerindex);
						f_move_distance_length = Math.sqrt((f_move_touched_posx - I_STATIC_MOVE_SMALL_CIRCLE_X)*(f_move_touched_posx - I_STATIC_MOVE_SMALL_CIRCLE_X)+(f_move_touched_posy - I_STATIC_MOVE_SMALL_CIRCLE_Y)*(f_move_touched_posy - I_STATIC_MOVE_SMALL_CIRCLE_Y));
						if(f_move_distance_length > I_STATIC_SMALL_CIRCLE_RADIUS*2){
							f_move_distance_length = I_STATIC_SMALL_CIRCLE_RADIUS*2;
						}
						o_main_character.setMoveAngle(Math.atan2(f_move_touched_posx - I_STATIC_MOVE_SMALL_CIRCLE_X, f_move_touched_posy - I_STATIC_MOVE_SMALL_CIRCLE_Y));
						i_move_small_circle_x = (float)(I_STATIC_MOVE_SMALL_CIRCLE_X + f_move_distance_length*Math.sin(o_main_character.getMoveAngle()));
						i_move_small_circle_y = (float)(I_STATIC_MOVE_SMALL_CIRCLE_Y + f_move_distance_length*Math.cos(o_main_character.getMoveAngle()));

						//caculate background move
						i_map_move_x_offset = (int)(f_move_distance_length/8*Math.sin(o_main_character.getMoveAngle()));
						i_map_move_y_offset = (int)(f_move_distance_length/8*Math.cos(o_main_character.getMoveAngle()));
						

					}
					if(i_fight_touched_pointerindex != -1){
						// System.out.println("Fight controlingggggggggggggggggggggggggggggggggggggg");
						// System.out.println("Fight Index 3: " + i_fight_touched_pointerindex + ", " + i_fight_touched_pointerid);
						// System.out.println("MotionEvent.ACTION_MOVE 2 " + e.getActionIndex() + ", " + e.getPointerId(e.getActionIndex()));
						f_fight_touched_posx = e.getX(i_fight_touched_pointerindex);
						f_fight_touched_posy = e.getY(i_fight_touched_pointerindex);
						f_fight_distance_length = Math.sqrt((f_fight_touched_posx - I_STATIC_FIGHT_SMALL_CIRCLE_X)*(f_fight_touched_posx - I_STATIC_FIGHT_SMALL_CIRCLE_X)+(f_fight_touched_posy - I_STATIC_FIGHT_SMALL_CIRCLE_Y)*(f_fight_touched_posy - I_STATIC_FIGHT_SMALL_CIRCLE_Y));
						if(f_fight_distance_length > I_STATIC_SMALL_CIRCLE_RADIUS*2){
							f_fight_distance_length = I_STATIC_SMALL_CIRCLE_RADIUS*2;
						}
						o_main_character.setGunAngle(Math.atan2(f_fight_touched_posx - I_STATIC_FIGHT_SMALL_CIRCLE_X, f_fight_touched_posy - I_STATIC_FIGHT_SMALL_CIRCLE_Y));
						i_fight_small_circle_x = (float)(I_STATIC_FIGHT_SMALL_CIRCLE_X + f_fight_distance_length*Math.sin(o_main_character.getGunAngle()));
						i_fight_small_circle_y = (float)(I_STATIC_FIGHT_SMALL_CIRCLE_Y + f_fight_distance_length*Math.cos(o_main_character.getGunAngle()));
						o_main_character.setGunPosition((float)((double)o_main_character.getX() + o_main_character.getActorGunLength()*Math.sin(o_main_character.getGunAngle())), (float)((double)o_main_character.getY() + o_main_character.getActorGunLength()*Math.cos(o_main_character.getGunAngle())));
						f_gun_virtual_target_x = (float)((double)o_main_character.getX() + 3*o_main_character.getActorGunLength()*Math.sin(o_main_character.getGunAngle()));
						f_gun_virtual_target_y = (float)((double)o_main_character.getY() + 3*o_main_character.getActorGunLength()*Math.cos(o_main_character.getGunAngle()));
						bulletlist.add(new Bullet(o_main_character.getGunPosX(), o_main_character.getGunPosY(), f_gun_virtual_target_x, f_gun_virtual_target_y));
					}
					
				}
				input_pointer_array = new int[i_touched_pointer_count];
				input_pointer_array_id = new int[i_touched_pointer_count];
				input_pointer_array_x = new float[i_touched_pointer_count];
				input_pointer_array_y = new float[i_touched_pointer_count];
				for(int i = 0; i < i_touched_pointer_count; i ++){
					input_pointer_array_id[i] = e.getPointerId(i);
					input_pointer_array_x[i] = e.getX(i);
					input_pointer_array_y[i] = e.getY(i);
				}
			break;
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent e){
		super.onTouchEvent(e);
		Process_TouchEvent(e);		
		return true;
	}
	
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		Initialize();
	}
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
	}
	
	@Override
	public void surfaceDestroyed(SurfaceHolder holder){
		boolean retry = true;
		i_thread_can_run = false;
        do {
            try {
                o_game_mainthread.join();
                retry = false;
            } catch (InterruptedException e) {
				e.printStackTrace();
                // we will try it again and again...
            }
        }while(retry);
		
	}
	
	@Override
	public void run(){
		while(i_thread_can_run){
			if (!b_is_game_pause){
				try{
					// Thread.sleep(20);
				}catch(Exception e){}
				Game_Update();
				Canvas o_lockcanvas = o_surfaceholder.lockCanvas(null);
				synchronized (o_surfaceholder) {
					if(o_lockcanvas != null){
						Game_Draw(o_lockcanvas);
						o_surfaceholder.unlockCanvasAndPost(o_lockcanvas);
					}
				}
			}
		}
	}
	/*
	design:
	tim thiet ke game structure moi
	add animation logo
	khi ban trung object, object se doi mau dam nhat de tao hieu ung, 
	tao hieu ung trung dan nhu pewpew
	moi lan ban trung se duoc 1 diem
	duoc 100 diem se +1 missile laught
	moi lan laught se co nhieu missile xuat phat tu vong quanh MC
	lam hieu ung lock taget neu duoc
	neu tren man hinh het target thi missiles se mat dieu khien va di chuyen thang, neu tren duong di ma gap mat cu object nao cung se no
	*/
}